import java.util.HashMap;
import java.util.HashSet;

class Solution {
    public int totalFruit(int[] fruits) {
        int left = 0, right = 0;
        int len = 0;
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        while (right < fruits.length) {
            hashMap.put(fruits[right], hashMap.getOrDefault(fruits[right], 0) + 1);
            while (hashMap.size() > 2) {
                hashMap.put(fruits[left], hashMap.get(fruits[left]) - 1);
                if (hashMap.get(fruits[left]) == 0) {
                    hashMap.remove(fruits[left]);
                }
                left++;
            }
            len = Math.max(len, right - left + 1);
            right++;
        }
        return len;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.totalFruit(new int[]{0, 1, 6, 6, 4, 4, 6}));
    }
}